import java.util.List;
import java.util.ArrayList;

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

//leetcode 257 二叉树的所有路径
public class BinaryTreePaths {
    private List<String> list;

    public List<String> binaryTreePaths(TreeNode root) {
        list = new ArrayList<>();
        dfs(root, new StringBuilder());
        return list;
    }

    private void dfs(TreeNode root, StringBuilder path) {
        StringBuilder sb = new StringBuilder(path);
        sb.append(root.val);
        if (root.left == null && root.right == null) {
            list.add(sb.toString());
            return;
        }
        sb.append("->");
        if (root.left != null) dfs(root.left, sb);
        if (root.right != null) dfs(root.right, sb);
    }
}
